package com.lzj.test;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Environment;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.morgoo.droidplugin.pm.PluginManager;
import com.morgoo.helper.compat.PackageManagerCompat;

import java.io.File;

public class MainActivity extends AppCompatActivity {
    private TextView tvTest;
    private File[] plugins;
    private String filepath = null, packageName = "com.lzj.plugin";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tvTest = (TextView) findViewById(R.id.tv_test);
        Button btnTest = (Button) findViewById(R.id.btn_test);
        Button btn_delete = (Button) findViewById(R.id.btn_delete);
        Button btn_open = (Button) findViewById(R.id.btn_open);

        File file = new File(Environment.getExternalStorageDirectory(), "/plugin");
        plugins = file.listFiles();
        //没有插件
        if (plugins == null || plugins.length == 0) {
            return;
        }
        filepath = plugins[0].getAbsolutePath();
        tvTest.setText(plugins[0].getAbsolutePath());
        btnTest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (filepath == null) {
                    Toast.makeText(MainActivity.this, "filepath is null", Toast.LENGTH_SHORT).show();
                    return;
                }
                String result = null;
                int code;
                try {
                    code = PluginManager.getInstance().installPackage(filepath, PackageManagerCompat.INSTALL_REPLACE_EXISTING);
                    result = "install";
                    Log.i("code ", "" + code);
                    switch (code) {
                        case PluginManager.INSTALL_FAILED_NO_REQUESTEDPERMISSION:
                            result = "安装失败，文件请求的权限太多";
                            break;
//                                case PackageManagerCompat.INSTALL_FAILED_NOT_SUPPORT_ABI:
//                                     result = "宿主不支持插件的abi环境，可能宿主运行时为64位，但插件只支持32位";
//                                     break;
                        case PackageManagerCompat.INSTALL_SUCCEEDED:
                            result = "安装完成";
                            PackageManager pm = getPackageManager();
                            Intent intent = pm.getLaunchIntentForPackage(packageName);
                            if (intent == null) {
                                Toast.makeText(v.getContext(), "intent is null", Toast.LENGTH_SHORT).show();
                                return;
                            }
                            //向宿主传值，intent传值
                            Bundle bundle  = new Bundle();
                            bundle.putString("lzj","宿主传的东西");
                            intent.putExtras(bundle);
                            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent);
                            break;
                    }

                } catch (RemoteException e) {
                    result = "安装失败 " + e.getMessage();
                }
                tvTest.setText(result);
            }
        });

        btn_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    PluginManager.getInstance().deletePackage(packageName, 0);
                    tvTest.setText("del successs");
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        });

        btn_open.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    PackageManager pm = getPackageManager();
                    Intent intent = pm.getLaunchIntentForPackage(packageName);
                    if (intent == null) {//null为没安装
                        tvTest.setText("please install plugin");
                        if (filepath == null) {
                            Toast.makeText(MainActivity.this, "filepath is null", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        int code = PluginManager.getInstance().installPackage(filepath, PackageManagerCompat.INSTALL_REPLACE_EXISTING);
                        String result = "install";
                        Log.i("code ", "" + code);
                        switch (code) {
                            case PluginManager.INSTALL_FAILED_NO_REQUESTEDPERMISSION:
                                result = "安装失败，文件请求的权限太多";
                                break;
//                                case PackageManagerCompat.INSTALL_FAILED_NOT_SUPPORT_ABI:
//                                     result = "宿主不支持插件的abi环境，可能宿主运行时为64位，但插件只支持32位";
//                                     break;
                            case PackageManagerCompat.INSTALL_SUCCEEDED:
                                result = "安装完成";
                                intent = pm.getLaunchIntentForPackage(packageName);
                                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                startActivity(intent);
                                break;
                        }
                        tvTest.setText(result);
                    } else {//安装好了的
                        intent = pm.getLaunchIntentForPackage(packageName);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);
                        tvTest.setText("open successs");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

    }
}
